import { AppDataSource } from "../data-source";
import { NextFunction, Request, Response } from "express";
import { PhotoMetadata } from "../entity/PhotoMetadata.";

export class PhotoMetadataController {
  private photoMetadataService = AppDataSource.getRepository(PhotoMetadata);

  async one(request: Request, response: Response, next: NextFunction) {
    const id = request.params.id;
    const photoMetadata = await this.photoMetadataService.findOne({
      where: { id },
      relations: ["photo"],
    });
    console.log("PhotoMetadataController", photoMetadata);
    if (!photoMetadata) {
      return response.status(404).json({ message: "Photo metadata not found" });
    }
    return response.json(photoMetadata);
  }

  async remove(request: Request, response: Response, next: NextFunction) {
    const id = parseInt(request.params.id);

    let photoMetadataToRemove = await this.photoMetadataService.findOne({
      where: { id },
      relations: ["photo"],
    });
    console.log(photoMetadataToRemove);
    if (!photoMetadataToRemove) {
      return "this photo metadata does not exist";
    }

    await this.photoMetadataService.remove(photoMetadataToRemove);

    console.log(photoMetadataToRemove);

    return "photo metadata has been removed";
  }
}
